home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / comm / tcp / hserv.lha / hserv / main / hserv.rexx < prev   
OS/2 REXX Batch file  |  1999-05-24  |  13KB  |  373 lines

  1. /* hserv.rexx - hserv.rexx main server macro */
  2.  
  3. signal on halt
  4. signal on break_c
  5.  
  6. call addLibs
  7. global.prg=ProgramName("NOEXT")
  8. call checkStack
  9. call SysLogCtl(global.prg)
  10. call readArguments
  11. call initGlobal
  12. call initServerPort
  13. call SysLogCtl(global.prg "("global.port")")
  14. call checkInetd
  15. call initPort
  16. call initNotify
  17. call initSocket
  18. call initCx
  19. call info("Started")
  20. t=GetClip("HCONF_"global.PortName)
  21. if t~="" then call signal(x2d(t),2**15)
  22. call handleServer
  23. call fin
  24. exit
  25. /***************************************************************************/
  26. handleServer: procedure expose global.
  27.     sel.read.0=global.sock
  28.     open=1
  29.     sigWait=or(2**12,global.ps,global.ns,global.cxs)
  30.     do while open
  31.         res = WaitSelect("SEL",,,sigWait)
  32.         if and(sel.signals,2**12)~=0 then call fin
  33.         if res=1 then do
  34.             rsock=accept(global.sock,"REMOTE")
  35.             if rsock>=0 then call handle(rsock)
  36.         end
  37.         if and(sel.signals,global.ns)~=0 then do
  38.             if readConfig(global.configFile)~=0 then call fin
  39.         end
  40.         if and(sel.signals,global.ps)~=0 then do
  41.             pkt=GetPkt(global.PortName)
  42.             do while pkt~=null()
  43.                 if ~handlePort(pkt) then call fin
  44.                 pkt=GetPkt(global.PortName)
  45.             end
  46.         end
  47.         if and(sel.signals,global.cxs)~=0 then open=CxHandle()
  48.     end
  49.     return
  50. /***************************************************************************/
  51. handle: procedure expose global.
  52. parse arg sock
  53.     if global.status~="CLOSED" then call RXSCall("hs",sock)
  54.     call CloseSocket(sock)
  55.     return
  56. /***************************************************************************/
  57. addLibs: procedure expose global.s
  58.     l="rmh.library";if ~show("L",l) then;if ~addlib(l,0,-30) then exit
  59.     if AddLibrary("rexxsupport.library","rxsocket.library")~=0 then exit
  60.     return
  61. /***************************************************************************/
  62. checkStack: procedure expose global.
  63.     if ~IsLibOn("SOCKET") then do
  64.         call EasyRequest("No TCP/IP stack is running."d2c(10)"A bsdsocket.library compatible"d2c(10)"internet stack must be started"d2c(10)"before you can run hserv.")
  65.         exit
  66.     end
  67.     if IsLibOn("TTCP") then do
  68.         call EasyRequest("Sorry, due Termite doesn't have"d2c(10)"ReleaseCopyOfSocket() function"d2c(10)"hserv can't run on it.")
  69.         exit
  70.     end
  71.     return
  72. /***************************************************************************/
  73. readArguments: procedure expose global.
  74.     parm.0.value="/conf/hserv.conf"
  75.     if ~RMH_ReadArgs("FILE,PORT/N,QUIET/s") then call err DosString()
  76.     if parm.1.flag then
  77.         if parm.1.value<0 | parm.1.value>65535 then call err "bad PORT value '"parm.1.value"'"
  78.         else global.argsPort=parm.1.value
  79.     else global.argsPort=""
  80.     global.configFile=parm.0.value
  81.     global.quiet=parm.2.flag
  82.     return
  83. /***************************************************************************/
  84. initGlobal: procedure expose global.
  85.     call pragma(D,PathPart(ProgramName("FULL")))
  86.     if global.configFile=="" then global.configFile="/conf/hserv.conf"
  87.     global.configFile=RName(global.configFile)
  88.     global.ver    = 13.1
  89.     global.author = "Alfonso Ranieri"
  90.     global.hotkey = "rawkey ctrl alt h"
  91.  
  92.     if readConfig(global.configFile)~=0 then exit
  93.     call pragma("P",global.pri)
  94.  
  95.     return
  96. /***************************************************************************/
  97. initServerPort: procedure expose global.
  98.     if global.argsPort~="" then global.port=global.argsPort
  99.     if global.port=0 then
  100.         if GetServByName("S","http","tcp") then global.port=s.servPort
  101.         else global.port=80
  102.     call SetVar("hserv_Port",global.port,"LOCAL")
  103.     return
  104. /***************************************************************************/
  105. checkInetd: procedure expose global.
  106.     ls=LastSocket()
  107.     if ls>=0 then do
  108.         call handle(ls)
  109.         exit
  110.     end
  111.     return
  112. /***************************************************************************/
  113. initPort: procedure expose global.
  114.     global.PortName="HSERV."global.port
  115.     if ~OpenPort(global.portname) then call err "can't create port"
  116.     if ~SetVar("hserv_PortName",global.portname,"LOCAL") then do
  117.         call err("error setting vars")
  118.         exit
  119.     end
  120.     global.ps=PortSignal(global.PortName)
  121.     return
  122. /***************************************************************************/
  123. handlePort: procedure expose global.
  124. parse arg pkt
  125.     comm=GetArg(pkt)
  126.     open=1
  127.     res=0
  128.     select
  129.         when upper(comm)=="QUIT" then open=0
  130.         when upper(left(comm,4))=="CONF" then do
  131.             parse var comm c file .
  132.             if file~="" then do
  133.                 call FreeNotify(global.noti)
  134.                 global.configFile=RName(file)
  135.                 call initNotify
  136.                 call info("Changed config" global.configFile)
  137.             end
  138.             if readConfig(global.configFile)~=0 then call fin
  139.         end
  140.         when upper(left(comm,4))=="SHOW" then call RxsCall("hconf" '"'global.configFile'"' SERVER global.PortName,-1)
  141.         otherwise res=15
  142.     end
  143.     call Reply(pkt,res)
  144.     return open
  145. /***************************************************************************/
  146. initNotify: procedure expose global.
  147.     global.noti=StartNotify(global.configFile)
  148.     if global.noti=-1 then call err "can't create notify ("DosString()")"
  149.     global.ns=NotifySignal(global.noti)
  150.     return
  151. /***************************************************************************/
  152. initSocket: procedure expose global.
  153.     global.sock=socket("INET","STREAM")
  154.     if global.sock<0 then call err "can't create socket %m"
  155.  
  156.     local.addrFamily="INET"
  157.     local.addrPort=global.port
  158.     if bind(global.sock,"LOCAL")<0 then call err "can't bind socket %m"
  159.     if listen(global.sock,5)<0 then call err "can't listen on socket %m"
  160.     return
  161. /***************************************************************************/
  162. initCx: procedure expose global.
  163.     global.cxi=AddCx(global.portname,global.prg global.ver "©" global.author,"http ARexx server [port" global.port"]","SHOWHIDE",global.hotkey)
  164.     global.cxs=CxSignal(global.cxi)
  165.     return
  166. /**************************************************************************/
  167. CxHandle: procedure expose global.
  168.     handle.wait=0
  169.     num=HandleCx(global.cxi,"HANDLE")
  170.     do i=0 to num-1
  171.         select
  172.             when handle.i.class=="KILL" then return 0
  173.             when handle.i.class=="APPEAR" | handle.i.class=="HOTKEY" then do
  174.                 t=GetClip("HCONF_"global.PortName)
  175.                 if t="" then call RxsCall("hconf" '"'global.configFile'"' SERVER global.PortName,-1)
  176.                 else call signal(x2d(t),2**14)
  177.             end
  178.             when handle.i.class=="DISAPPEAR" then do
  179.                 t=GetClip("HCONF_"global.PortName)
  180.                 if t~="" then call signal(x2d(t),2**13)
  181.             end
  182.             when handle.i.class=="UNABLE" then do
  183.                 global.status="OPENED"
  184.                 call SetVar("hserv_Status",global.Status,"LOCAL")
  185.             end
  186.             when handle.i.class=="DISABLE" then do
  187.                 global.status="CLOSED"
  188.                 call SetVar("hserv_Status",global.Status,"LOCAL")
  189.             end
  190.             otherwise nop
  191.         end
  192.     end
  193.     return 1
  194. /**************************************************************************/
  195. fin: procedure expose global.
  196.     call ClosePort(global.portname)
  197.     t=GetClip("HCONF_"global.PortName)
  198.     if t~="" then call signal(x2d(t),2**15)
  199.     call info("Closed")
  200.     exit
  201. /***************************************************************************/
  202. err: procedure expose global.
  203. parse arg msg
  204.     call SysLog(msg,"ERR")
  205.     exit
  206. /***************************************************************************/
  207. info: procedure expose global.
  208. parse arg msg
  209.     if ~global.quiet then call SysLog(msg,"INFO")
  210.     return
  211. /***************************************************************************/
  212. readConfig: procedure expose global.
  213. parse arg file
  214.     global.HostName                = ""
  215.     global.Port                    = 80
  216.     global.Pri                    = 0
  217.     global.Status                = "OPENED"
  218.     global.DocumentDir            = "/htdocs"
  219.     global.DocumentIndex        = "index.html"
  220.     global.CgiDir                = ""
  221.     global.TransferLog            = "OFF"
  222.     global.TransferFile            = "/logs/transfers.log"
  223.     global.ErrorLog                = "SYS"
  224.     global.ErrorFile            = "/logs/error.log"
  225.     global.Auth                    = "/conf/identify"
  226.     global.RejectedIP            = "/conf/rejected"
  227.     global.MimeFile                = "/conf/mime"
  228.     global.KeepAlive            = 1
  229.     global.KeepAliveTimeout        = 100
  230.     global.Timeout                = 300
  231.     global.HostNameLookups        = "OFF"
  232.     global.Ident                = "OFF"
  233.     global.OnlyAmigaClient        = "OFF"
  234.     global.DefImage                = "ON"
  235.     global.Admin                = ""
  236.     global.Specials                = ""
  237.     global.Handlers                = ""
  238.     global.Errors                = ""
  239.     global.VirtualHosts            = ""
  240.  
  241.     lines=ParseConfig(file,"CONF")
  242.     if lines==-1 then do
  243.         call err("Config file" file "not found")
  244.         return 4
  245.     end
  246.  
  247.     err=0
  248.     do i=0 to lines-1 while err=0
  249.         opt=conf.i
  250.         arg=conf.i.value
  251.         argu=upper(arg)
  252.         select
  253.             when opt=="HOSTNAME" then HostName=arg
  254.             when opt=="PORT" then
  255.                 if ~DataType(arg,"N") then err=2
  256.                 else if arg<1 | arg>65535 then err=2
  257.                      else global.Port=arg
  258.             when opt=="STATUS" then
  259.                 if argu~="OPENED" & argu~= "PAUSED" & argu~= "CLOSED" then err=1
  260.                 else global.Status=argu
  261.             when opt=="PRI" then
  262.                 if ~DataType(arg,"N") then err=2
  263.                 else if arg<-128 | arg>128 then err=2
  264.                      else global.Pri=arg
  265.             when opt=="DOCUMENTDIR" then global.DocumentDir=arg
  266.             when opt=="DOCUMENTINDEX" then global.DocumentIndex=arg
  267.             when opt=="CGIDIR" then global.CgiDir=arg
  268.             when opt=="AUTH" then global.Auth=arg
  269.             when opt=="HOSTNAMELOOKUPS" then
  270.                 if argu~="ON" & argu~= "OFF" then err=1
  271.                 else global.HostNameLookups=argu
  272.             when opt=="ERRORLOG" then
  273.                 if argu~="ON" & argu~= "OFF" & argu~="SYS" then err=1
  274.                 else global.ErrorLog=argu
  275.             when opt=="ERRORFILE" then ErrorFile=arg
  276.             when opt=="TRANSFERLOG" then
  277.                 if argu~="ON" & argu~= "OFF" & argu~="SYS" then err=1
  278.                 else global.TransferLog=argu
  279.             when opt=="TRANSFERFILE" then TransferFile=arg
  280.             when opt=="IDENT" then
  281.                 if argu~="ON" & argu~= "OFF" then err=1
  282.                 else global.ident=argu
  283.             when opt=="KEEPALIVE" then
  284.                 if ~DataType(arg,"N") then err=2
  285.                 else global.KeepAlive=arg
  286.             when opt=="KEEPALIVETIMEOUT" then
  287.                 if ~DataType(arg,"N") then err=2
  288.                 else global.KeepAliveTimeout=arg
  289.             when opt=="TIMEOUT" then
  290.                 if ~DataType(arg,"N") then err=2
  291.                 else global.Timeout=arg
  292.             when opt=="REJECTEDIP" then global.RejectedIP=arg
  293.             when opt=="MIMEFILE" then global.MimeFile=arg
  294.             when opt=="ONLYAMIGACLIENT" then
  295.                 if argu~="ON" & argu~= "OFF" then err=1
  296.                 else global.OnlyAmigaClient=argu
  297.             when opt=="DEFIMAGE" then
  298.                 if argu~="ON" & argu~= "OFF" then err=1
  299.                 else global.DefImage=argu
  300.             when opt=="ADMIN" then global.admin=arg
  301.             when opt=="SPECIALS" then global.Specials=arg
  302.             when opt=="HANDLERS" then global.Handlers=arg
  303.             when opt=="ERRORS" then global.Errors=arg
  304.             when opt=="VIRTUALHOSTS" then global.VirtualHosts=arg
  305.             otherwise err=3
  306.         end
  307.     end
  308.     i=i-1
  309.  
  310.     select
  311.         when err==1 then call err("bad option '"arg"' in line" conf.i.line)
  312.         when err==2 then call err("bad number '"arg"' in line" conf.i.line)
  313.         when err==3 then call err("unknown option '"opt"' in line" conf.i.line)
  314.         when err==4 then call err("bad argument '"arg"' in line" conf.i.line)
  315.         otherwise nop
  316.     end
  317.     if err~=0 then return err
  318.  
  319.     if global.ErrorFile=="" & global.ErrorLog~="SYS" then global.ErrorLog="OFF"
  320.     if global.TransferFile=="" & global.TransferLog~="SYS" then global.TransferLog="OFF"
  321.     if global.RejectedIP~="" then global.HostNameLookups="ON"
  322.  
  323.     pre="hserv_"
  324.     err=SetVar(pre"HostName",global.HostName,"LOCAL")-1+,
  325.     SetVar(pre"Ver",global.ver,"LOCAL")-1+,
  326.     SetVar(pre"Port",global.port,"LOCAL")-1+,
  327.     SetVar(pre"DocumentDir",global.DocumentDir,"LOCAL")-1+,
  328.     SetVar(pre"DocumentIndex",global.DocumentIndex,"LOCAL")-1+,
  329.     SetVar(pre"CgiDir",global.CgiDir,"LOCAL")-1+,
  330.     SetVar(pre"HostNameLookups",global.HostNameLookups,"LOCAL")-1+,
  331.     SetVar(pre"ErrorLog",global.ErrorLog,"LOCAL")-1+,
  332.     SetVar(pre"ErrorFile",global.ErrorFile,"LOCAL")-1+,
  333.     SetVar(pre"TransferLog",global.TransferLog,"LOCAL")-1+,
  334.     SetVar(pre"TransferFile",global.TransferFile,"LOCAL")-1+,
  335.     SetVar(pre"Auth",global.Auth,"LOCAL")-1+,
  336.     SetVar(pre"KeepAlive",global.KeepAlive,"LOCAL")-1+,
  337.     SetVar(pre"KeepAliveTimeout",global.KeepAliveTimeout,"LOCAL")-1+,
  338.     SetVar(pre"Timeout",global.Timeout,"LOCAL")-1+,
  339.     SetVar(pre"RejectedIP",global.RejectedIP,"LOCAL")-1+,
  340.     SetVar(pre"Ident",global.Ident,"LOCAL")-1+,
  341.     SetVar(pre"MimeFile",global.MimeFile,"LOCAL")-1+,
  342.     SetVar(pre"OnlyAmigaClient",global.OnlyAmigaClient,"LOCAL")-1+,
  343.     SetVar(pre"DefImage",global.DefImage,"LOCAL")-1+,
  344.     SetVar(pre"ConfigFile",global.configFile,"LOCAL")-1+,
  345.     SetVar(pre"Pri",global.Pri,"LOCAL")-1+,
  346.     SetVar(pre"Connections",global.Connections,"LOCAL")-1+,
  347.     SetVar(pre"Status",global.Status,"LOCAL")-1+,
  348.     SetVar(pre"Specials",global.Specials,"LOCAL")-1+,
  349.     SetVar(pre"Handlers",global.Handlers,"LOCAL")-1+,
  350.     SetVar(pre"Errors",global.Errors,"LOCAL")-1+,
  351.     SetVar(pre"VirtualHosts",global.VirtualHosts,"LOCAL")-1+,
  352.     SetVar(pre"Admin",global.Admin,"LOCAL")-1
  353.  
  354.     if err~=0 then do
  355.         call err("error setting vars")
  356.         return 5
  357.     end
  358.     else return 0
  359. /***************************************************************************/
  360. RName: procedure
  361. parse arg file
  362.     od=pragma("D")
  363.     call pragma("D",pathpart(file))
  364.     file=AddPart(pragma("D"),filepart(file))
  365.     od=pragma("D",od)
  366.     return file
  367. /***************************************************************************/
  368. halt:
  369. break_c:
  370.     call fin
  371. /***************************************************************************/
  372. /*$VER: hserv.rexx 13.1 (17.5.99)*/
  373.